weiXinController.controller('kgyOrderEditCtl',['$scope','$state','$stateParams','$timeout','$interval','$compile','$rootScope',function($scope,$state,$stateParams,$timeout,$interval,$compile,$rootScope) {

    var id = EdeyeAdmin.unzip($stateParams.param).id;
    $scope.id = id;

    EdeyeAdmin.post({
        url:'/orderInfo/getOrderBalanceRecordById',
        data:{
            id:id,
            status:'INPUT'
        }
    },{
        SUCCESS:function(ret){
            var order = ret.order;
            $scope.warehouse_info_id = order.warehouse_info_id;
            $scope.warehouse_show = order.name;
            $scope.type = order.type;
            $scope.approval_type = order.approval_type;
            $scope.materiel_account_type = order.type_account;
            var listName = 'materiel_' + $scope.materiel_account_type.toLowerCase() + '_' + $scope.materiel_type.toLowerCase() + '_list';
            $scope[listName] = ret.data;
            for(var tmpKey in $scope[listName]){
                if($scope[listName][tmpKey].type == 'RFID'){
                    $scope[listName][tmpKey].rfid = JSON.parse($scope[listName][tmpKey].rfid);
                    $scope[listName][tmpKey].qr = [];
                    $scope[listName][tmpKey].basket = {};
                }else if($scope[listName][tmpKey].type == 'QR'){
                    $scope[listName][tmpKey].basket = {};
                    $scope[listName][tmpKey].rfid = [];
                    // $scope[listName][tmpKey].qr = JSON.parse($scope[listName][tmpKey].qr);
                    // 19-02-19 16:41:00 改成如下
                    $scope[listName][tmpKey].qr = JSON.parse($scope[listName][tmpKey].materiel_info);
                }else {
                    $scope[listName][tmpKey].rfid = [];
                    $scope[listName][tmpKey].qr = [];
                }
            }
            $scope.$apply();
        }
    });

	// 测试$scope.$apply使用
	$scope.title = '修改申领单';

    $scope.materiel_type = 'BALANCE';

    // 账外冲销备件列表
    $scope.materiel_out_balance_list = [];

    // 账内冲销备件列表
    $scope.materiel_in_balance_list = [];

    $scope.type_account
	
	// 相关说明
	$scope.showDes = function(){
		layer.open({
			type: 1,
			skin: 'layui-layer-rim',
			area: ['420px', '240px'],
			title: '说明',
			content: `
				<div class="row" style="margin-left: 0px; margin-right: 0px; background-color: white;">
					<div class="col-sm-12 mt-3 mb-3">
						申领单没有冲销类型的借出申领单:<br />
						实际数量<系统记录数量<br /><br />
						冲销会在后台解绑相关硬件<br />
					</div>
				</div>
				`
		});
	}

    // 准备添加的备件绑定信息(modal中展示)
    $scope.bind_info = {
        sp_code:'',
        batch_code:'',
        type:'',
        unit:'',
        num:0,
        shelf_parent:{},
        shelf:{},
        rfid:[],
        rfid_show:'',
        qr:[],
        qr_show:'',
        basket_parent:{},
        basket:{},
        show_rfid:false,
        show_basket:false
    }

    // 弹出入库单备件新增modal
    $scope.addStoreMaterielModal = function(){
        $('#addStoreMaterielModal').modal({backdrop: 'static', keyboard: false});
    }

    // 关闭入库单新增modal
    $scope.closeStoreMaterielModal = function(){
        $scope.bind_info = {
            sp_code:'',
            batch_code:'',
            type:'',
            unit:'',
            num:0,
            shelf_parent:{},
            shelf:{},
            rfid:[],
            rfid_show:'',
            qr:[],
            qr_show:'',
            basket_parent:{},
            basket:{},
            show_rfid:false,
            show_basket:false
        }
        $('#addStoreMaterielModal').modal('hide');
    }

    // sp 数据获取
    $scope.spPageSearch = function(currentPage,pageSize){
        var postData = {materiel_account_type:$scope.materiel_account_type};
        postData.currentPage = (currentPage === undefined ? 1 : currentPage);
        postData.pageSize = (pageSize === undefined ? EdeyeAdmin.getPageSizePhone(spPageParam.id) : pageSize);
        EdeyeAdmin.post({
            url:'/storeInfo/getAllMaterielGt0Page',
            data:postData
        },{
            SUCCESS:function(ret){
                if(ret.code === '200'){
                    $scope.prefix = ret.prefix;
                    EdeyeAdmin.setDataTableDataPhone(spPageParam, ret.data);
                }
            }
        });
    }

    // SP号分页初始化条件
    var spPageParam = {
        id : 'spTable',
        checkbox:false,
        column : [
            {
                type:'rowNum',
                title:'编号',
                titleTdStyle: 'width: 100px;color: #17a2b8;'
            },{
                type:'key',
                mapping:'id',
                show:false
            },{
            	type:'?',
				title:'名称',
				trans:function(data){
					return data.name;
				}
            },{
            	type:'?',
				title:'单位',
				trans:function(data){
					return data.unit;
				}
            },{
            	type:'?',
				title:'CP编号',
				trans:function(data){
					return data.sp_code;
				}
            },{
            	type:'?',
				title:'分类类型',
				trans:function(data){
					if(data.type == 'WEIGHT') {
						 return "称重";
					} else if(data.type == 'QR') {
						return "二维码";
					}else{
						 return data.type;
					}
				}
            },{
            	type:'?',
				title:'能否借用',
				trans:function(data){
					if(data.borrow === 'CAN'){
                		return '可以借用';
                	}
                	if(data.borrow === 'CANT'){
                		return '不可借用';
                	}
                	return showMsg;
				}
            },{
                type: 'function',
                title: '操作',
                titleDivStyle: 'text-align: center',
                dataDivStyle: 'text-align: center',
                func: [{
                    title: '选择',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-check"></i>',
                    text: '选择',
                    func: function (data) {
                        $scope.callBackSelectSpModal(data);
                    }
                }]
            }
        ],
		pageConfig:{
			showGoNum:false
		},
        pageSize:[[5],10,50,100],
        page:function(cmd, currentPage, pageSize, totalPageNum){
            if('first' === cmd){
                currentPage = 1;
            }else if('prev' === cmd){
                currentPage--;
            }else if('next' === cmd){
                currentPage++;
            }else if('last' === cmd){
                currentPage = totalPageNum;
            }else if('change' === cmd){
                currentPage = 1;
            }
            $scope.spPageSearch(currentPage,pageSize);
        }
    };
    EdeyeAdmin.initDataTablePhone(spPageParam, {data:[]});

    // 弹出SP号的modal
    $scope.openSelectSpModal = function(){
        $scope.spPageSearch();
        $('#selectSpModal').modal('show');
    }

    // 选择SP号后数据回填,并关闭modal
    $scope.callBackSelectSpModal = function(data){
        if($scope.bind_info.sp_code){
            // TODO 二次确认 注释 4月8日
            // layer.confirm('更换SP号后将会重新选择备件', {
            //     btn: ['更换','不更换']
            // }, function(){
                $scope.$apply(function(){
                    $scope.bind_info.sp_code = data.sp_code;
                    $scope.bind_info.type = data.type;
                    if($scope.bind_info.type === 'WEIGHT'){
                        $scope.bind_info.type_view = '称重';
                        $scope.bind_info.show_rfid = false;
                        $scope.bind_info.show_basket = true;
                    }
                    if($scope.bind_info.type === 'QR'){
                        $scope.bind_info.type_view = '二维码';
                        $scope.bind_info.show_rfid = false;
                        $scope.bind_info.show_basket = false;
                    }
                    if($scope.bind_info.type === 'RFID'){
                        $scope.bind_info.type_view = data.type;
                        $scope.bind_info.show_rfid = true;
                        $scope.bind_info.show_basket = false;
                    }
                    if($scope.materiel_type === 'IN'){
                        $scope.bind_info.show_rfid = false;
                        $scope.bind_info.show_basket = false;
                    }

                    $scope.bind_info.batch_code = '';
                    $scope.bind_info.unit = data.unit;

                    $scope.bind_info.shelf = {};
                    $scope.bind_info.shelf_parent = {};

                    $scope.bind_info.basket = {};
                    $scope.bind_info.basket_parent= {};

                    $scope.bind_info.rfid = [];
                    $scope.bind_info.rfid_show = '';
                });
                layer.closeAll();
            // }, function(){
            // });
            $('#selectSpModal').modal('hide');
        }else {
            $scope.$apply(function(){
                $scope.bind_info.sp_code = data.sp_code;
                $scope.bind_info.type = data.type;
                if($scope.bind_info.type === 'WEIGHT'){
                    $scope.bind_info.type_view = '称重';
                    $scope.bind_info.show_rfid = false;
                    $scope.bind_info.show_basket = true;
                }
                if($scope.bind_info.type === 'QR'){
                    $scope.bind_info.type_view = '二维码';
                    $scope.bind_info.show_rfid = false;
                    $scope.bind_info.show_basket = false;
                }
                if($scope.bind_info.type === 'RFID'){
                    $scope.bind_info.type_view = data.type;
                    $scope.bind_info.show_rfid = true;
                    $scope.bind_info.show_basket = false;
                }
                if($scope.materiel_type === 'IN'){
                    $scope.bind_info.show_rfid = false;
                    $scope.bind_info.show_basket = false;
                }

                $scope.bind_info.batch_code = '';
                $scope.bind_info.unit = data.unit;
            });
            layer.closeAll();
            $('#selectSpModal').modal('hide');
        }

    }


    // 批次号分页初始化条件
    var batchPageParam = {
        id : 'batchTable',
        checkbox:false,
        column : [
            {
                type:'rowNum',
                title:'编号',
            },{
                type:'key',
                mapping:'id',
                show:false
            },{
                mapping:'batch_code',
                title:'批次号'
            },{
                type: 'function',
                title: '操作',
                titleDivStyle: 'text-align: center',
                dataDivStyle: 'text-align: center',
                func: [{
                    title: '选择',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-check"></i>',
                    text: '选择',
                    func: function (data) {
                        $scope.callBackSelectBatchModal(data);
                    }
                }]
            }
        ],
		pageConfig:{
			showGoNum:false
		},
        pageSize:[[5],10,50,100],
        page:function(cmd, currentPage, pageSize, totalPageNum){
            if('first' === cmd){
                currentPage = 1;
            }else if('prev' === cmd){
                currentPage--;
            }else if('next' === cmd){
                currentPage++;
            }else if('last' === cmd){
                currentPage = totalPageNum;
            }else if('change' === cmd){
                currentPage = 1;
            }
            $scope.batchPageSearch(currentPage,pageSize);
        }
    };
    EdeyeAdmin.initDataTablePhone(batchPageParam, {data:[]});

    $scope.batchPageSearch = function(currentPage,pageSize){
        var postData = {materiel_account_type:$scope.materiel_account_type,sp_code:$scope.bind_info.sp_code};
        postData.currentPage = (currentPage === undefined ? 1 : currentPage);
        postData.pageSize = (pageSize === undefined ? EdeyeAdmin.getPageSizePhone(batchPageParam.id) : pageSize);
        EdeyeAdmin.post({
            url:'/storeInfo/getAllMaterielTypeStockGt0Page',
            data:postData
        },{
            SUCCESS:function(ret){
                if(ret.code === '200'){
                    $scope.prefix = ret.prefix;
                    EdeyeAdmin.setDataTableDataPhone(batchPageParam, ret.data);
                }
            }
        });
    }

    // 弹出批次号号的modal
    $scope.openSelectBatchModal = function(){
        if($scope.bind_info.sp_code.length < 1){
            EdeyeAdmin.closeMsg('请先选择CP号',true,1);
            return;
        }
        $scope.batchPageSearch();
        $('#selectBatchModal').modal('show');
    }

    // 选择批次号号后数据回填,并关闭modal
    $scope.callBackSelectBatchModal = function(data){
        $scope.$apply(function(){
            $scope.bind_info.batch_code = data.batch_code;

            $scope.bind_info.shelf = {};
            $scope.bind_info.shelf_parent = {};
            $scope.bind_info.basket_parent= {};
            $scope.bind_info.basket = {};
        });
        $('#selectBatchModal').modal('hide');
    }


    // RFID分页初始化条件
    var rfidPageParam = {
        id : 'rfidTable',
        column : [
            {
                type:'rowNum',
                title:'编号',
            },{
                type:'key',
                mapping:'id',
                show:false
            },{
                mapping:'name',
                title:'备件绑定的RFID名称'
            },{
                mapping:'code',
                title:'备件绑定的RFID编码'
            },{
                mapping:'gmt_modified',
                title:'RFID绑定时间'
            }
        ],
		pageConfig:{
			showGoNum:false
		},
        pageSize:[[5],10,50,100,1000],
        page:function(cmd, currentPage, pageSize, totalPageNum){
            if('first' === cmd){
                currentPage = 1;
            }else if('prev' === cmd){
                currentPage--;
            }else if('next' === cmd){
                currentPage++;
            }else if('last' === cmd){
                currentPage = totalPageNum;
            }else if('change' === cmd){
                currentPage = 1;
            }
            $scope.rfidPageSearch(currentPage,pageSize);
        }
    };
    EdeyeAdmin.initDataTablePhone(rfidPageParam, {data:[]});

    $scope.rfidPageSearch = function(currentPage,pageSize){
        var postData = {sp_code:$scope.bind_info.sp_code, batch_code:$scope.bind_info.batch_code, type_account:$scope.materiel_account_type, type_device:'RFID'};
        postData.currentPage = (currentPage === undefined ? 1 : currentPage);
        postData.pageSize = (pageSize === undefined ? EdeyeAdmin.getPageSizePhone(rfidPageParam.id) : pageSize);
        EdeyeAdmin.post({
            url:'/orderInfo/getAllRfidTypePage',
            data:postData
        },{
            SUCCESS:function(ret){
                if(ret.code === '200'){
                    $scope.prefix = ret.prefix;
                    EdeyeAdmin.setDataTableDataPhone(rfidPageParam, ret.data);
                }
            }
        });
    }

    // 弹出选择rfid标签的modal
    $scope.openSelectRfidModal = function(){
        if(!$scope.bind_info.batch_code){
            EdeyeAdmin.closeMsg('请先选择批次号',true,1);
            return;
        }
        $scope.rfidPageSearch();
        $('#selectRfidModal').modal('show');
    }

    // 选择rfid标签后回调的方法
    $scope.callBackSelectRfidModal = function(){
        var selectData = EdeyeAdmin.getDataTableSelectPhone('rfidTable');
        if(selectData.length < 1){
            EdeyeAdmin.closeMsg('请选择备件',true,1);
            return;
        }
        $scope.bind_info.rfid = selectData;
        for(var tmpKey in selectData){
            if(tmpKey === '0'){
                $scope.bind_info.rfid_show = selectData[tmpKey].name;
            }else{
                $scope.bind_info.rfid_show += ' | ' + selectData[tmpKey].name;
            }
        }
        $('#selectRfidModal').modal('hide');
    }

    // 货架货位树形配置
    var shelfTreeConfig = {
        treeId: 'shelfTree',
        view: {
            showLine: true,
            selectedMulti: false,
            dblClickExpand: false,
            addDiyDom: function (treeId, treeNode) {
                var aObj = $("#" + treeNode.tId + '_a');
                var msg = (treeNode.level === 0 ? '货架' : '货位');
                aObj.append('<span>[' + msg + ']</span>');
            }
        },
        data: {
            simpleData: {
                enable: true,
                idKey: 'id',
                pIdKey: 'pid'
            }
        },
        edit: {
            enable: false
        },
        check: {
            enable: false
        }
    };
    var shelfTree = null;

    // 弹出选择货位的modal
    $scope.openSelectShelfModal = function(){
        if($scope.bind_info.sp_code.length < 1){
            EdeyeAdmin.closeMsg('请先选择CP号',true,1);
            return;
        }

        if(!$scope.bind_info.batch_code){
            EdeyeAdmin.closeMsg('请先选择批次号',true,1);
            return;
        }

        shelfTree = $.fn.zTree.init($("#shelfTree"), shelfTreeConfig);

        EdeyeAdmin.post({
            url: '/orderInfo/getAllShelfTreeDataByParams',
            data: {type:$scope.bind_info.type,sp_code:$scope.bind_info.sp_code, batch_code:$scope.bind_info.batch_code, type_account:$scope.materiel_account_type, type_device:'WEIGHT'}
        }, {
            SUCCESS: function (ret) {
            	if(ret.code == 500){
                    EdeyeAdmin.closeMsg(ret.msg,true,1);
                    return;
				}
                shelfTree.addNodes(null, ret.data);
                setTimeout(function () {
                    shelfTree.expandAll(true);
                }, 200);

                $('#selectShelfModal').modal('show');
            }
        });
    }

    // 选择货位后回调方法
    $scope.callBackSelectShelfModal = function(){
        var nodes = shelfTree.getSelectedNodes();
        if(nodes.length < 1){
            EdeyeAdmin.closeMsg('请选择货位',true,1);
            return;
        }
        if(nodes[0].level === 0){
            EdeyeAdmin.closeMsg('请选择货位',true,1);
            return;
        }

        $scope.bind_info.shelf.id = nodes[0].id;
        $scope.bind_info.shelf.name = nodes[0].name;
        $scope.bind_info.shelf_parent.id = nodes[0].getParentNode().id;
        $scope.bind_info.shelf_parent.name = nodes[0].getParentNode().name;

        $scope.bind_info.rfid = [];
        $scope.bind_info.rfid_show = '';

        $('#selectShelfModal').modal('hide');
    }

    // 称,物料盒树形配置
    var weighTreeConfig = {
        treeId: 'weighTree',
        view: {
            showLine: true,
            selectedMulti: false,
            dblClickExpand: false,
            addDiyDom: function (treeId, treeNode) {
                var aObj = $("#" + treeNode.tId + '_a');
                var msg = (treeNode.level === 0 ? '称' : '物料盒');
                aObj.append('<span>[' + msg + ']</span>');
            }
        },
        data: {
            simpleData: {
                enable: true,
                idKey: 'id',
                pIdKey: 'pid'
            }
        },
        edit: {
            enable: false
        },
        check: {
            enable: false
        }
    };
    var weighTree = null;

    // 弹出选择物料盒的modal
    $scope.openSelectBasketModal = function(){
        if($scope.bind_info.shelf.id === undefined){
            EdeyeAdmin.closeMsg('请先选择货位',true,1);
            return;
        }

        weighTree = $.fn.zTree.init($("#weighTree"), weighTreeConfig);

        EdeyeAdmin.post({
            url: '/orderInfo/getAllWeightTreeDataByParams',
            data: {sp_code:$scope.bind_info.sp_code,batch_code:$scope.bind_info.batch_code,type_account:$scope.materiel_account_type,shelf_id:$scope.bind_info.shelf.id}
        }, {
            SUCCESS: function (ret) {
                weighTree.addNodes(null, ret.data);
                setTimeout(function () {
                    weighTree.expandAll(true);
                }, 200);

                $('#selectBasketModal').modal('show');
            }
        });
    }

    // 选择物料盒后回调方法
    $scope.callBackSelectBasketModal = function(){
        var nodes = weighTree.getSelectedNodes();
        if(nodes.length < 1){
            EdeyeAdmin.closeMsg('请选择物料盒',true,1);
            return;
        }
        if(nodes[0].level === 0){
            EdeyeAdmin.closeMsg('请选择物料盒',true,1);
            return;
        }

        $scope.bind_info.basket.id = nodes[0].id;
        $scope.bind_info.basket.name = nodes[0].name;
        $scope.bind_info.basket_parent.id = nodes[0].getParentNode().id;
        $scope.bind_info.basket_parent.name = nodes[0].getParentNode().name;

        $('#selectBasketModal').modal('hide');
    }


    // 二维码分页初始化条件
    var qrPageParam = {
        id : 'qrTable',
        column : [
            {
                type:'rowNum',
                title:'编号',
            },{
                type:'key',
                mapping:'id',
                show:false
            },{
                mapping:'id',
                title:'二维码'
            }
        ],
		pageConfig:{
			showGoNum:false
		},
        pageSize:[[5],10,50,100,1000],
        page:function(cmd, currentPage, pageSize, totalPageNum){
            if('first' === cmd){
                currentPage = 1;
            }else if('prev' === cmd){
                currentPage--;
            }else if('next' === cmd){
                currentPage++;
            }else if('last' === cmd){
                currentPage = totalPageNum;
            }else if('change' === cmd){
                currentPage = 1;
            }
            $scope.qrPageSearch(currentPage,pageSize);
        }
    };
    EdeyeAdmin.initDataTablePhone(qrPageParam, {data:[]});

    //获取二维码数据
    $scope.qrPageSearch = function(currentPage,pageSize){
        var postData = {type_account:$scope.materiel_account_type,sp_code:$scope.bind_info.sp_code, batch_code:$scope.bind_info.batch_code};
        postData.currentPage = (currentPage === undefined ? 1 : currentPage);
        postData.pageSize = (pageSize === undefined ? EdeyeAdmin.getPageSizePhone(batchPageParam.id) : pageSize);
        EdeyeAdmin.post({
            url:'/orderInfo/getAllQrPageByParams',
            data:postData
        },{
            SUCCESS:function(ret){
                if(ret.code === '200'){
                    $scope.prefix = ret.prefix;
                    EdeyeAdmin.setDataTableDataPhone(qrPageParam, ret.data);
                }
            }
        });
    }

    //二维码
    $scope.openSelectQrModal = function () {
        if(!$scope.bind_info.batch_code){
            EdeyeAdmin.closeMsg('请先选择批次号',true,1);
            return;
        }

        $scope.qrPageSearch();
        $('#selectQrModal').modal('show');
    }

    // 选择二维码标签后回调的方法
    $scope.callBackSelectQrModal = function(){
        var selectData = EdeyeAdmin.getDataTableSelectPhone('qrTable');
        if(selectData.length < 1){
            EdeyeAdmin.closeMsg('请选择二维码标签',true,1);
            return;
        }

        var temp = [];

        for(var index in selectData){
            temp.push(selectData[index].id);
        }

        $scope.bind_info.qr = temp;
        for(var tmpKey in selectData){
            if(tmpKey === '0'){
                $scope.bind_info.qr_show = selectData[tmpKey].id;
            }else{
                $scope.bind_info.qr_show += ' | ' + selectData[tmpKey].id;
            }
        }
        $('#selectQrModal').modal('hide');
    }

    // 多层modal必须使用这个方法,否则会造成上1层的modal失去滚动条
    $('#selectSpModal,#selectBatchModal,#selectShelfModal,#selectRfidModal,#selectBasketModal').on('hidden.bs.modal', function (event) {
        $("body").addClass("modal-open");
    });


    // 新增入库单备件并关闭modal
    $scope.addStoreMateriel = function(){
        // 实际/冲销校验SP号,批次号,新增数量
        var ret = EdeyeAdmin.checkKit.finalCheck($scope.bind_info, true, {
            'sp_code': {
                method: 'strRange',
                methodParam: ['&&', ['>=', 1], ['<=', 128]],
                msg: '请选择CP号'
            },
            'batch_code': {
                method: 'strRange',
                methodParam: ['&&', ['>=', 1], ['<=', 128]],
                msg: '请选择CP号'
            },
            'num': {
                method: 'integerRange',
                methodParam: ['&&', ['>=', 1], ['<=', 1000000]],
                msg: '冲销数量在1~1000000'
            }
        });
        if (!ret.flag) {
            EdeyeAdmin.closeMsg(ret.msg.join('<br />'), false, 1);
            return;
        }


        // WEIGHT
        if($scope.bind_info.type === 'WEIGHT'){
            // 冲销入库校验是否选择货位
            if($scope.bind_info.shelf.id === undefined){
                EdeyeAdmin.closeMsg('请选择货位',true,1);
                return;
            }

            if($scope.bind_info.basket.id === undefined){
                EdeyeAdmin.closeMsg('请选择称重筐',true,1);
                return;
            }
        }

        // RFID
        if($scope.bind_info.type === 'RFID'){
            if($scope.bind_info.rfid.length < 1){
                EdeyeAdmin.closeMsg('请选择RFID标签',true,1);
                return;
            }
            if($scope.bind_info.rfid.length != $scope.bind_info.num){
                EdeyeAdmin.closeMsg('已选择的RFID标签个数与冲销数量不符',true,1);
                return;
            }
            var listName = 'materiel_' + $scope.materiel_account_type.toLowerCase() + '_' + $scope.materiel_type.toLowerCase() + '_list';
            for(var tmpKey in $scope[listName]){
                if($scope[listName][tmpKey].type === 'RFID'){
                    var addedRfids = $scope[listName][tmpKey].rfid;
                    for(var tmpInnerKey in addedRfids){
                        for(var tmpInInnerKey in $scope.bind_info.rfid){
                            if(addedRfids[tmpInnerKey].id === $scope.bind_info.rfid[tmpInInnerKey].id){
                                EdeyeAdmin.closeMsg('该申领单已存在此备件，请重新选择',true,1);
                                return;
                            }
                        }
                    }
                }
            }
        }

        //二维码
        if($scope.bind_info.type === 'QR'){
            if($scope.bind_info.qr.length < 1){
                EdeyeAdmin.closeMsg('请选择二维码标签',true,1);
                return;
            }
            if($scope.bind_info.qr.length != $scope.bind_info.num){
                EdeyeAdmin.closeMsg('已选择的二维码标签个数与冲销数量不符',true,1);
                return;
            }
            var listName = 'materiel_' + $scope.materiel_account_type.toLowerCase() + '_' + $scope.materiel_type.toLowerCase() + '_list';
            for(var tmpKey in $scope[listName]){
                if($scope[listName][tmpKey].type === 'QR'){
                    var addedQrs = $scope[listName][tmpKey].qr;
                    for(var tmpInnerKey in addedQrs){
                        for(var tmpInInnerKey in $scope.bind_info.qr){
                            if(addedQrs[tmpInnerKey] === $scope.bind_info.qr[tmpInInnerKey]){
                                EdeyeAdmin.closeMsg('该申领单已存在此备件，请重新选择',true,1);
                                return;
                            }
                        }
                    }
                }
            }
        }


        var listName = 'materiel_' + $scope.materiel_account_type.toLowerCase() + '_' + $scope.materiel_type.toLowerCase() + '_list';
        $scope[listName].push({
            id:moment().format('YYMMDDHHmmssSSS'),
            sp_code:$scope.bind_info.sp_code,
            batch_code:$scope.bind_info.batch_code,
            type:$scope.bind_info.type,
            num:$scope.bind_info.num,
            unit:$scope.bind_info.unit,
            type_name:($scope.bind_info.type === 'WEIGHT' ? '称重' : ($scope.bind_info.type === 'QR' ? '二维码' : $scope.bind_info.type)),
            shelf_parent:$scope.bind_info.shelf_parent,
            shelf:$scope.bind_info.shelf,
            rfid:$scope.bind_info.rfid,
            basket_parent:$scope.bind_info.basket_parent,
            basket:$scope.bind_info.basket,
            qr:$scope.bind_info.qr
        });

        console.info()
        $scope.closeStoreMaterielModal();
    }

    // 已加入到入库单中的备件查看时的ng1对象
    $scope.materielViewObj = {};



    // 提交入库单
    $scope.submitStore = function(){
        var listName = 'materiel_' + $scope.materiel_account_type.toLowerCase() + '_' + $scope.materiel_type.toLowerCase() + '_list';
        console.info($scope[listName]);
        if($scope[listName].length < 1){
            EdeyeAdmin.closeMsg('请添加备件',true,1);
            return;
        }

        // 冲销校验
        // RFID备件不可重复绑定
        var rfids = [];
        for(var tmpKey in $scope[listName]){
            if($scope[listName][tmpKey].type === 'RFID'){
                var rfids_ = $scope[listName][tmpKey].rfid;
                for(var tmpInnerKey in rfids_){
                    rfids.push(rfids_[tmpInnerKey].id);
                }
            }
        }
        if(rfids.length > 0){
            if((new Set(rfids)).size != rfids.length){
                EdeyeAdmin.closeMsg('有重复绑定的RFID',true,1);
                return;
            }
        }
        // TODO 二次确认 注释 4月8日
        // layer.confirm('是否提交入库单?', {
        //     btn: ['确认','取消'],
        //     closeBtn: false
        // }, function(){
            EdeyeAdmin.post({
                url: '/orderInfo/updateOrderByBalance',
                data: {
                    id:$scope.id,
                    param:$scope[listName]
                }
            }, {
                SUCCESS: function (ret) {
                    EdeyeAdmin.closeMsg(ret.msg,true,1);
                }
            });
            layer.closeAll();
        // });
    }

    // 移出加入到入库单中的备件
    $scope.removeStoreMateriel = function(id){
        // TODO 二次确认 注释 4月8日
        // layer.confirm('是否将备件从列表中移出?', {
        //     btn: ['确认','取消'],
        //     closeBtn: false
        // }, function(){
            var listName = 'materiel_' + $scope.materiel_account_type.toLowerCase() + '_' + $scope.materiel_type.toLowerCase() + '_list';
            for(var tmpKey in $scope[listName]){
                if($scope[listName][tmpKey].id === id){
                    // $scope.$apply(function(){
                        $scope[listName].splice(tmpKey, 1);
                    // });
                    break;
                }
            }
            layer.closeAll();
        //     }, function(){
        //     }
        // );
    }

    // 查看加入到入库单中的备件详情
    $scope.viewStoreMateriel = function(id){
        var listName = 'materiel_' + $scope.materiel_account_type.toLowerCase() + '_' + $scope.materiel_type.toLowerCase() + '_list';
        for(var tmpKey in $scope[listName]){
            if($scope[listName][tmpKey].id === id){
                $scope.materielViewObj = $scope[listName][tmpKey];
                break;
            }
        }
        if($scope.materielViewObj.rfid.length > 0){
            $scope.materielViewObj.show_rfid = true;
            $scope.materielViewObj.show_basket = false;

            for(var tmpKey in $scope.materielViewObj.rfid){
                if(tmpKey === '0'){
                    $scope.materielViewObj.rfid_show = $scope.materielViewObj.rfid[tmpKey].name;
                }else{
                    $scope.materielViewObj.rfid_show += ' | ' + $scope.materielViewObj.rfid[tmpKey].name;
                }
            }
        }
        if($scope.materielViewObj.qr.length > 0){
            $scope.materielViewObj.show_rfid = false;
            $scope.materielViewObj.show_basket = false;

            for(var tmpKey in $scope.materielViewObj.qr){
                if(tmpKey === '0'){
                    // $scope.materielViewObj.qr_show = $scope.materielViewObj.qr[tmpKey].id;
                	// 19-02-19 16:45:00 改成如下
                    $scope.materielViewObj.qr_show = $scope.materielViewObj.qr[tmpKey];
                }else{
                    // $scope.materielViewObj.qr_show += ' | ' + $scope.materielViewObj.qr[tmpKey].id;
                	// 19-02-19 16:45:00 改成如下
                    $scope.materielViewObj.qr_show += ' | ' + $scope.materielViewObj.qr[tmpKey];
                }
            }
        }

        if($scope.materielViewObj.basket.id !== undefined){
            $scope.materielViewObj.show_basket = true;
            $scope.materielViewObj.show_rfid = false;
        }

        $('#viewMaterielModal').modal('show');
    }

}]);

weiXinFilter.filter('materielTypeFilter', function () {
    return function (input) {
        if(input == 'WEIGHT'){
            return '称重';
        }else if(input == 'QR'){
            return '二维码';
        }else {
            return input;
        }
    }
});